import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: "/", redirect: "/login" }, // 重定向到/login
    {
      path: "/manager",
      name: "manager",
      component: () => import("../views/Manager.vue"),
      // 路由嵌套
      //在嵌套路由里面子路由不用加/，会自动添加
      children: [
        {
          path: "home",
          name: "home",
          meta: { title: "首页" },
          component: () => import("../views/Home.vue"),
        },
        {
          path: "test",
          name: "test",
          meta: { title: "测试页" },
          component: () => import("../views/Test.vue"),
        },
        {
          path: "data",
          name: "data",
          meta: { title: "数据展示页" },
          component: () => import("../views/Data.vue"),
        },
        {
          path: "employee",
          name: "employee",
          meta: { title: "员工信息页" },
          component: () => import("../views/Employee.vue"),
        },
        {
          path: "admin",
          name: "admin",
          meta: { title: "管理员信息" },
          component: () => import("../views/Admin.vue"),
        },
        {
          path: "person",
          name: "person",
          meta: { title: "个人信息" },
          component: () => import("../views/Person.vue"),
        },
        {
          path: "password",
          name: "password",
          meta: { title: "修改密码" },
          component: () => import("../views/Password.vue"),
        },
      ],
    },
    {
      path: "/login",
      name: "Login",
      meta: { title: "登录系统" },
      component: () => import("../views/Login.vue"),
    },
    {
      path: "/register",
      name: "Register",
      meta: { title: "欢迎注册" },
      component: () => import("../views/Register.vue"),
    },
    {
      path: "/404",
      name: "NotFound",
      meta: { title: "404找不到页面" },
      component: () => import("../views/404.vue"),
    },
    { path: "/:pathMatch(.*)*", redirect: "/404" },
    // 匹配所有未定义的路由，重定向到404
    // name不能重复
  ],
});
//beforeEach表示跳转之前的一些操作
router.beforeEach((to, from, next) => {
  document.title = to.meta.title // 设置页面标题
  next() // 必须调用next()方法，否则路由不会跳转
});

export default router
